# 使用 node:18-alpine 作为构建阶段的基础镜像
FROM node:18-alpine as builder
WORKDIR /my-space

# 复制 package.json 和 package-lock.json 并安装依赖
COPY package.json package-lock.json ./
RUN npm ci

# 复制所有文件并构建应用
COPY . .
RUN npm run build

# 使用 node:18-alpine 作为运行阶段的基础镜像
FROM node:18-alpine as runner
WORKDIR /my-space

# 从构建阶段复制必要的文件
COPY --from=builder /my-space/.next/standalone ./
COPY --from=builder /my-space/.next/static ./.next/static
COPY --from=builder /my-space/public ./public
COPY --from=builder /my-space/next.config.mjs ./

# 安装运行时依赖
# 注意: 如果您有运行时依赖，请取消注释下面的两行
# COPY --from=builder /my-space/package.json /my-space/package-lock.json ./
# RUN npm ci --only=production

# 暴露 3000 端口
EXPOSE 3000

# 设置运行时的启动命令
CMD ["node", "server.js"]